var count = 1
var container = document.getElementById('container')
function getUserAction(e) {
    container.innerHTml = count++
    console.log(count)
    console.log(this)
    console.log(e) //JavaScript 在事件处理函数中会提供事件对象 event如果我们不使用 debouce 函数，这里会打印 MouseEvent 对象
}
container.onmousemove = debounce(getUserAction, 2000)

// function throttle(fn,time) {
//     var previous = 0
//     return function() {
//         var now = +new Date()
//         var context,args
//         context  = this
//         args = arguments
//         if(now - previous > time) {
//             fn.apply(context,args)
//             previous = now;
//         }
//     }

// }
function throttle(fn,time) {
    var timeout;
    return function() {
        var context,args
            context  = this
            args = arguments
        if(!timeout) {
            timeout = setTimeout(function() {
                timeout = null
                fn.apply(context,args)
            },time)
        }
    }
}
function debounce(fn,time) {
    var timeout;
    var context,args
    return function() {
        context = this
        args = arguments
        clearTimeout(timeout)
        setTimeout( function() {
            fn.apply(context,args)
        },time)
    }
}


// 设置定时器，当定时器存在就不执行，不存在就执行
function thorttle (tiem,fn) {
    let timer
    return function() {
        let _self = this
        let args  = arguments
        if(!timer) {
            timer = setTimeout(function() {
                fn.apply(_self,args)
                timer= null
            },tiem)
        }
    }
   
}